package com.bdqn.boot.yuanshen.controller;

import com.bdqn.boot.yuanshen.bean.Users;
import com.bdqn.boot.yuanshen.bean.Videos;
import com.bdqn.boot.yuanshen.service.VideosService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Hasee
 * @version 1.0
 * @className VideosCotroller
 * @description TODD
 * @date 2025/3/17 10:34
 */
@Slf4j
@RestController
@CrossOrigin
public class VideosCotroller {
    @Autowired
    private VideosService videosService;
    @GetMapping("/Videos/Page")
    public PageInfo<Videos> getVideos(@RequestParam(defaultValue = "1") int pageNum,
                                      @RequestParam(defaultValue = "10") int pageSize) {
        return videosService.getVideosPage(pageNum, pageSize);
    }

    @GetMapping("/Videos")
    public List<Videos> getVideos() {
        return videosService.getVideos();
    }

    @PostMapping("/Videos")
    public String addVideos(@RequestBody Videos videos) {
        if (videos.getId()!=null) {
            videosService.updateVideos(videos);
            return "update";

        }else {
            videosService.addVideos(videos);
            return "success";
        }
    }

    @DeleteMapping("/Videos/{id}")
    public String deleteUsers(@PathVariable int id) {
        videosService.deleteVideos(id);
        return "delete";
    }

    @GetMapping("/Videos/{id}")
    public Videos getVideos(@PathVariable Integer id) {
        return  videosService.getVideosId(id);
    }

    @PostMapping("/VideosId")
    public Map<String,Object> getVideosByUserId(@RequestBody Users users) {
        Map<String,Object> map = new HashMap<>();
        try {
            List<Videos> list = videosService.getVideosByUserId(users.getId());
            map.put("videos",list);
            map.put("success", true);
            map.put("msg", "加载视频封面成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("msg", "加载视频封面失败");
        }
     return map;
    }

}
